package com.gsy.HW510;

import java.util.InputMismatchException;
import java.util.Scanner;

public class Bank {

    public static void main(String[] args) {
        System.out.println("----Welcome IIT Bank----");
        recur();
    }
    public static void recur(){

        Scanner sc = new Scanner(System.in);
        AccountHolder accountHolder;
        while (true){
            System.out.println("Please enter your initial balance:");
            double temp = sc.nextDouble();
            if (temp>0){
                accountHolder = AccountHolder.createAccount(temp,"user");
                break;
            }
            System.err.println("初始余额不应该为负值");;
            continue;
        }
        while(true) {
            System.out.println("Please select the transaction:");
            System.out.println("1.Deposit; 2.Withdraw ;3.Check balance; 4.Exit");

            int num;
            try {
                num = sc.nextInt();
            }catch (InputMismatchException e){
                System.err.println("输入错误");
                continue;
            }
            if (num == 4){
                System.out.println("----Thanks!----");
                break;
            }
            double temp;
            try {
                switch(num) {
                    case 1:
                        System.out.println("Please enter money to be deposited: ");
                        temp=sc.nextDouble();
                        if (accountHolder.saveMoney(temp)) {
                            System.out.println("Your Money has been successfully deposited");
                        }else {
                            System.err.println("存钱不能为负值");
                        }
                        break;
                    case 2:
                        System.out.println("Please enter money to be withdrawn:");
                        temp =sc.nextDouble();
                        if (accountHolder.takeMoney(temp)) {
                            System.out.println("取钱成功");
                        }else {
                            System.err.println("余额不足");
                        }
                        break;
                    case 3:
//                    System.out.println("Dear,your balance:"+ balance);
                        System.out.println(accountHolder.getBalance());
                        break;
                    default:
                        System.out.println("Please enter correct Number!");
                }
            }catch (InputMismatchException e){
                System.err.println("输入错误");
            }

        }


    }
}
